package com.yidu.ruifeng_logistics.service.impl;

import com.yidu.ruifeng_logistics.dao.LogisticsModelDao;
import com.yidu.ruifeng_logistics.dao.LogisticsRoleModelDao;
import com.yidu.ruifeng_logistics.entity.LogisticsModel;
import com.yidu.ruifeng_logistics.entity.LogisticsRole;
import com.yidu.ruifeng_logistics.dao.LogisticsRoleDao;
import com.yidu.ruifeng_logistics.entity.LogisticsRoleModel;
import com.yidu.ruifeng_logistics.entity.LogisticsStaff;
import com.yidu.ruifeng_logistics.service.LogisticsRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (LogisticsRole)表服务实现类
 *
 * @author 张皓超
 * @since 2021-03-09 14:29:08
 */
@Service("logisticsRoleService")
public class LogisticsRoleServiceImpl implements LogisticsRoleService {
    Logger logger= LoggerFactory.getLogger(this.getClass());
    @Resource
    private LogisticsRoleDao logisticsRoleDao;

    @Resource
    private LogisticsRoleModelDao logisticsRoleModelDao;

    @Resource
    private LogisticsModelDao logisticsModelDao;
    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Override
    public LogisticsRole queryById(Integer roleId) {
        return this.logisticsRoleDao.queryById(roleId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<LogisticsRole> queryAllByLimit(int offset, int limit) {
        return this.logisticsRoleDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param logisticsRole 实例对象
     * @return 实例对象
     */
    @Override
    public LogisticsRole insert(LogisticsRole logisticsRole) {
        this.logisticsRoleDao.insert(logisticsRole);
        return logisticsRole;
    }

    /**
     * 修改数据
     *
     * @param logisticsRole 实例对象
     * @return 实例对象
     */
    @Override
    public LogisticsRole update(LogisticsRole logisticsRole) {
        this.logisticsRoleDao.update(logisticsRole);
        return this.queryById(logisticsRole.getRoleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer roleId) {
        return this.logisticsRoleDao.deleteById(roleId) > 0;
    }

    @Override
    public List<LogisticsRole> queryAll(LogisticsRole logisticsRole) {
        return this.logisticsRoleDao.queryAll(logisticsRole);
    }

    @Override
    @Transactional//事务
    public int addAndUpdate(Integer roleId,String roleName,String roleRemark, String name) {
        int i=0;
        LogisticsRole role=new LogisticsRole();
        role.setRoleName(roleName);
        role.setRoleRemark(roleRemark);
        if (name!=null&&name!="") {
            String[] split = name.split(",");
            logger.info(name+"===============");
            if (roleId != null) {
                //修改
                role.setRoleId(roleId);
                i = logisticsRoleDao.update(role);
                i = logisticsRoleModelDao.deleteroleId(roleId);
                    for (int i1 = 0; i1 < split.length; i1++) {
                        if (logisticsModelDao.selectModelParentIsNull(Integer.valueOf(split[i1]))>0){
                            LogisticsRoleModel roleModel=new LogisticsRoleModel();
                            roleModel.setRoleId(roleId);
                            roleModel.setModelId(Integer.valueOf(split[i1]));
                            i = logisticsRoleModelDao.insert(roleModel);
                        }else {
                            List<LogisticsModel> logisticsModels1 = logisticsModelDao.selectParentIsNoNull();
                            for (LogisticsModel l:logisticsModels1) {
                                if (l.getModelId()==Integer.valueOf(split[i1])){
                                    LogisticsRoleModel roleModel=new LogisticsRoleModel();
                                    roleModel.setRoleId(roleId);
                                    roleModel.setModelId(Integer.valueOf(split[i1]));
                                    i = logisticsRoleModelDao.insert(roleModel);
                                }
                            }
                        }
                    }

            } else {
                //增加
                i = logisticsRoleDao.insert(role);
                List<LogisticsModel> logisticsModels = logisticsModelDao.selectParentIsNull();
                    for (int i1 = 0; i1 < split.length; i1++) {
                        if (logisticsModelDao.selectModelParentIsNull(Integer.valueOf(split[i1]))>0){
                            LogisticsRoleModel roleModel=new LogisticsRoleModel();
                            roleModel.setRoleId(role.getRoleId());
                            roleModel.setModelId(Integer.valueOf(split[i1]));
                            i = logisticsRoleModelDao.insert(roleModel);
                        }else {
                            List<LogisticsModel> logisticsModels1 = logisticsModelDao.selectParentIsNoNull();
                            for (LogisticsModel l:logisticsModels1) {
                                if (l.getModelId()==Integer.valueOf(split[i1])){
                                    LogisticsRoleModel roleModel=new LogisticsRoleModel();
                                    roleModel.setRoleId(role.getRoleId());
                                    roleModel.setModelId(Integer.valueOf(split[i1]));
                                    i = logisticsRoleModelDao.insert(roleModel);
                                }
                            }
                        }
                    }
                }
            return i;
        }else{
            return i;
        }
    }

    @Override
    public List<LogisticsRole> findRoleByUser(LogisticsStaff staff) {
        return this.logisticsRoleDao.findRoleByUser(staff);
    }


}
